package com.china08.yxyapi.service.yxyresource.impl;

import org.apache.commons.lang3.StringUtils;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.TermQueryBuilder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import com.china08.yxyapi.entity.es.ElasticYxyResResource;
import com.china08.yxyapi.model.resource.SearchResourceModel;
import com.china08.yxyapi.service.yxyresource.YxyResourceBaseService;
import com.china08.yxyapi.service.yxyresource.YxyResourceSchService;

@Service
public class YxyResourceSchServiceImpl implements YxyResourceSchService {

    @Autowired
    private YxyResourceBaseService yxyResourceBaseService;

    @Override
    public Page<ElasticYxyResResource> manageSchPlatform(String schoolId, Integer status, String phase, String subject,
            String keyword, Pageable pageable) {
        BoolQueryBuilder boolQueryBuilder = new BoolQueryBuilder();
        // 刨除已删除的
        TermQueryBuilder builderDel = new TermQueryBuilder("isDelete", 0);
        boolQueryBuilder.must(builderDel);

        // 类别 viewScope 本校
        TermQueryBuilder builderviewScope = new TermQueryBuilder("viewScope", 1);
        boolQueryBuilder.must(builderviewScope);

        // 不查微课的
        TermQueryBuilder builderTypeNot = new TermQueryBuilder("typeId", "yx14");
        boolQueryBuilder.mustNot(builderTypeNot);

        // 学校
        TermQueryBuilder builderSchoolId = new TermQueryBuilder("uploaderSchoolId", schoolId);
        boolQueryBuilder.must(builderSchoolId);

        // 审核状态
        if (status != null && status != -1) {
            TermQueryBuilder builderStatusYes1 = new TermQueryBuilder("status", status);
            boolQueryBuilder.must(builderStatusYes1);
        }

        // 是否云校资源 isYxYunRes 不是云校资源
        TermQueryBuilder builderisYxYunRes = new TermQueryBuilder("isYxYunRes", 0);
        boolQueryBuilder.must(builderisYxYunRes);

        // 学段
        if (StringUtils.isNotBlank(phase) && !StringUtils.equals("all", phase)) {
            TermQueryBuilder builder = new TermQueryBuilder("phaseIdList", phase);
            boolQueryBuilder.must(builder);
        }
        // 学科
        if (StringUtils.isNotBlank(subject) && !StringUtils.equals("all", subject)) {
            TermQueryBuilder builder = new TermQueryBuilder("subjectIdList", subject);
            boolQueryBuilder.must(builder);
        }
        Page<ElasticYxyResResource> page = yxyResourceBaseService.getPageKeyword(boolQueryBuilder, keyword, 2,
                pageable);
        return page;
    }

    @Override
    public long notReviewedNum(SearchResourceModel searchResourceModel) {
        BoolQueryBuilder bool = new BoolQueryBuilder();
        String schoolId = searchResourceModel.schoolId;
        String phaseId = searchResourceModel.phaseId;
        String subjectId = searchResourceModel.subjectId;
        String keyword = searchResourceModel.keyword;
        Integer status = 0; // 未审核
        Integer viewScope = 1;
        Integer isYxYunRes = 0;
        bool.must(new TermQueryBuilder("isYxYunRes", isYxYunRes));
        bool.must(new TermQueryBuilder("viewScope", viewScope));
        bool.must(new TermQueryBuilder("status", status));
        bool.mustNot(new TermQueryBuilder("typeId", "yx14"));
        bool.must(new TermQueryBuilder("uploaderSchoolId", schoolId));
        // 学段
        if (StringUtils.isNotBlank(phaseId) && !StringUtils.equals("all", phaseId)) {
            bool.must(new TermQueryBuilder("phaseIdList", phaseId));
        }
        // 学科
        if (StringUtils.isNotBlank(subjectId) && !StringUtils.equals("all", subjectId)) {
            bool.must(new TermQueryBuilder("subjectIdList", subjectId));
        }
        BoolQueryBuilder b1 = new BoolQueryBuilder();
        b1.must(bool);
        long count = 0;
        if (StringUtils.isNotBlank(keyword)) {
            yxyResourceBaseService.keywordTU(keyword, 1, b1);
            count = yxyResourceBaseService.countResource(b1);
            if (count == 0) {
                BoolQueryBuilder b2 = new BoolQueryBuilder();
                b2.must(bool);
                yxyResourceBaseService.keywordTU(keyword, 2, b2);
                count = yxyResourceBaseService.countResource(b2);
            }
            if (count == 0) {
                BoolQueryBuilder b4 = new BoolQueryBuilder();
                b4.must(bool);
                yxyResourceBaseService.keywordTU(keyword, 4, b4);
                count = yxyResourceBaseService.countResource(b4);
            }
            if (count == 0) {
                BoolQueryBuilder b3 = new BoolQueryBuilder();
                b3.must(bool);
                yxyResourceBaseService.keywordTU(keyword, 3, b3);
                count = yxyResourceBaseService.countResource(b3);
            }
        } else {
            count = yxyResourceBaseService.countResource(b1);
        }

        return count;
    }

}
